package com.wang.tool.common;

import com.alibaba.fastjson.annotation.JSONField;
import com.wang.tool.util.ExceptionUtil;

import java.io.Serializable;

/**
 * 异常翻译结果
 *
 * @Author wangshaopeng
 * @Date 2022/6/20
 */
public class ErrorInfo implements Serializable {

    private static final long serialVersionUID = -3225484996818211897L;


    /**
     * 异常码
     */
    @JSONField(ordinal = 1)
    private String errorCode;

    /**
     * 异常信息
     */
    @JSONField(ordinal = 2)
    private String errorMsg;

    /**
     * 异常类型
     * 非空
     */
    @JSONField(ordinal = 3)
    private Class<? extends Throwable> originalType;

    /**
     * 原始信息
     */
    @JSONField(ordinal = 4)
    private String originalMsg;


    /**
     * 堆栈列表
     */
    @JSONField(ordinal = 5)
    private String stacks;


    public static ErrorInfo build(Throwable e, String errorCode, String errorMsg) {
        ErrorInfo error = new ErrorInfo();
        error.setOriginalType(e.getClass());
        error.setOriginalMsg(e.getMessage());
        error.setErrorCode(errorCode);
        error.setErrorMsg(errorMsg);
        error.setStacks(ExceptionUtil.formatErrorStackTrace(e));
        return error;
    }


    public Class<? extends Throwable> getOriginalType() {
        return originalType;
    }

    public void setOriginalType(Class<? extends Throwable> originalType) {
        this.originalType = originalType;
    }

    public String getErrorCode() {
        return errorCode;
    }

    public void setErrorCode(String errorCode) {
        this.errorCode = errorCode;
    }

    public String getErrorMsg() {
        return errorMsg;
    }

    public void setErrorMsg(String errorMsg) {
        this.errorMsg = errorMsg;
    }

    public String getOriginalMsg() {
        return originalMsg;
    }

    public void setOriginalMsg(String originalMsg) {
        this.originalMsg = originalMsg;
    }

    public String getStacks() {
        return stacks;
    }

    public void setStacks(String stacks) {
        this.stacks = stacks;
    }

    @Override
    public String toString() {
        return "errorCode:" + errorCode + ", errorMsg:" + errorMsg + ", stacks: " + stacks;
    }
}
